package com.rnny.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rnny.commonutils.R;
import com.rnny.entity.EduTeacher;
import com.rnny.entity.vo.TeacherQuery;
import com.rnny.service.EduTeacherService;
import com.rnny.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 软柠柠吖(Runny)
 * @date 2022-07-15
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;


    // 查询讲师表所有的数据
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    // 逻辑删除讲师的功能
    @DeleteMapping("/{id}")
    public R removeTeacher(@PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    // 分页查询讲师方法
    // current：当前页
    // size：每页记录数
    @GetMapping("/pageTeacher/{current}/{size}")
    public R pageListTeacher(@PathVariable("current") long current,
                             @PathVariable("size") long size) {
        // 创建 page 对象(com.baomidou.xxx包下的) —— 包米豆
        Page<EduTeacher> pageTeacher = new Page<>(current, size);
        // 分页
        // 调用方法时，底层封装，把分页所有的数据封装到 pageTeacher 对象中
        teacherService.page(pageTeacher, null);
        // 总记录数
        long total = pageTeacher.getTotal();
        // 数据 list 集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    // 条件查询带分页方法
    @PostMapping("/pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("size") long size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 创建 page 对象
        Page<EduTeacher> pageTeacher = new Page<>(current, size);

        // wrapper
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
        // 多条件组合查询
        wrapper.like(null != teacherQuery.getName(), EduTeacher::getName, teacherQuery.getName())
                .eq(null != teacherQuery.getLevel(), EduTeacher::getLevel, teacherQuery.getLevel())
                .ge(null != teacherQuery.getBegin(), EduTeacher::getGmtCreate, teacherQuery.getBegin())
                .le(null != teacherQuery.getEnd(), EduTeacher::getGmtCreate, teacherQuery.getEnd());
        // 调用方法实现条件查询分页
        teacherService.page(pageTeacher, wrapper);

        // 总记录数
        long total = pageTeacher.getTotal();
        // 数据 list 集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    // 添加讲师接口的方法
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }

    // 根据讲师 id 查询数据
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    // 讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }
}












